package com.lmf.annotation.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.EmbeddedValueResolverAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.PropertySource;
import org.springframework.util.StringValueResolver;

@Profile("lmf")  // @Profile注解除了能写到bean上，还能写到类上
@PropertySource("classpath:/dataSource.properties")
@Configuration
public class MyConfigOfProfile implements EmbeddedValueResolverAware {
    //手动声明一个字符串解析器
    private StringValueResolver valueResolver;

    //获取属性文件中的数据库连接数据
//    @Value("${pro.driver}")
    private String driver;
    @Value("${pro.urlTest}")
    private String urlTest;
    @Value("${pro.urlDev}")
    private String urlDev;
    @Value("${pro.urlProd}")
    private String urlProd;
    @Value("${pro.username}")
    private String username;
    @Value("${pro.password}")
    private String password;

    /**
     * @Profile("default"):定义了一个环境标识，只有当定义环境被激活以后，我们这个bean才能被注册进来;
     *      注意：1.环境标识表示可以取任意名称，大小写敏感，后面激活时，环境标识要完全一样
     */
    //注册德鲁伊数据源
    @Profile("default")  //在默认环境下创建
    @Bean("defaultDataSource")
    public DruidDataSource dataSourceTestDefault(){
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driver);
        dataSource.setUrl(urlDev);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return dataSource;
    }

    @Profile("test")  //在测试环境下创建
    @Bean("testDataSource")
    public DruidDataSource dataSourceTest(){
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driver);
        dataSource.setUrl(urlTest);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return dataSource;
    }

    @Profile("dev")  //在开发环境下创建
    @Bean("devDataSource")
    public DruidDataSource dataSourceDev(){
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driver);
        dataSource.setUrl(urlDev);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return dataSource;
    }
    @Profile("prod")  //在生产环境下创建
    @Bean("prodDataSource")
    public DruidDataSource dataSourceProd(){
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driver);
        dataSource.setUrl(urlProd);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return dataSource;
    }

    @Override
    public void setEmbeddedValueResolver(StringValueResolver resolver) {
        this.valueResolver=resolver;
        //通过字符串解析器，来获取属性文件的值；${pro.driver}会被解析，该解析器功能与@Value("${pro.driver}")类似
        driver = valueResolver.resolveStringValue("${pro.driver}");
    }
}
